/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef PAGPLAYERPROXY_H
#define PAGPLAYERPROXY_H
#include <ace/xcomponent/native_interface_xcomponent.h>
#include "napi/native_api.h"
#include <bits/alltypes.h>
#include <string>
#include "pag/pag.h"
#include <native_vsync/native_vsync.h>
#include "PagSurface.h"
#include <native_vsync/native_vsync.h>

namespace NativeXComponentSample {
//播放状态
enum EnumPagPlayState {
    E_PAG_PLAY_NULL,
    E_PAG_PLAY_PLAYING,
    E_PAG_PLAY_PAUSE,
    E_PAG_PLAY_COMPLETE,
    E_PAG_PLAY_STOP,
};

void SetupSurface(OH_NativeXComponent *component, void *nativeWindow, void *eglContext);
// extern pag::PAGPlayer *m_Pagplayer;
class PagPlayerProxy {
public:
    PagPlayerProxy();
    ~PagPlayerProxy();
    napi_value PagInit(napi_env env, napi_callback_info info);
    napi_value Prepare(napi_env env, napi_callback_info info);
    napi_value SetComposition(napi_env env, napi_callback_info info);
    napi_value Duration(napi_env env, napi_callback_info info);
    napi_value CurrentFrame(napi_env env, napi_callback_info info);
    napi_value GetProgress(napi_env env, napi_callback_info info);
    void Destructor(napi_env env, void *nativeObject, void *finalize); // 释放资源的函数(类似类的析构函数)
    napi_value SetSurface(napi_env env, napi_callback_info info);
    napi_ref sJSPlayerConstructor_; // 生命周期变量
    std::string _msg;               // 设置和获取数据的变量
    napi_env mEnv = nullptr;               // 记录环境变量
    napi_ref mRef = nullptr;               // 记录生命周期变量

public:
    //注册回调
    typedef void (*FuncReg)(napi_value total, napi_value cur);
    void RegFrameListener(FuncReg actFrame);

    napi_value GetState(napi_env env, napi_callback_info info);
    //暂停
    napi_value Pause(napi_env env, napi_callback_info info);
    //恢复播放
    napi_value Resume(napi_env env, napi_callback_info info);
    //停止播放
    napi_value Stop(napi_env env, napi_callback_info info);
    //添加动画播放状态监听
    napi_value AddPlayerListener(napi_env env, napi_callback_info info);

    napi_value SetRepeatCount(napi_env env, napi_callback_info info);

    napi_value SetProgress(napi_env env, napi_callback_info info);
    // 缩放与拉伸
    napi_value SetScaleMode(napi_env env, napi_callback_info info);
    // 返回当前缩放模式
    napi_value ScaleMode(napi_env env, napi_callback_info info);

    napi_value NativeSetMatrix(napi_env env, napi_callback_info info);
    // 设置cacheEnabled属性的值
    napi_value SetCacheEnabled(napi_env env, napi_callback_info info);
    // 设置cacheScale属性的值
    napi_value SetCacheScale(napi_env env, napi_callback_info info);
    // 设置渲染的max帧速率
    napi_value SetMaxFrameRate(napi_env env, napi_callback_info info);
    // 设置useDiskCache属性的值
    napi_value SetUseDiskCache(napi_env env, napi_callback_info info);
    // 设置videoEnabled属性的值
    napi_value SetVideoEnabled(napi_env env, napi_callback_info info);
    // 如果设置为true，PAG将把相关的渲染数据缓存到磁盘文件中，例如视频合成的解码图像帧
    napi_value UseDiskCache(napi_env env, napi_callback_info info);
    // 如果设置为false，PAGView将跳过视频合成的渲染
    napi_value VideoEnabled(napi_env env, napi_callback_info info);
    // 渲染的max帧速率
    napi_value MaxFrameRate(napi_env env, napi_callback_info info);
    // 如果设置为true，PAG渲染器将缓存每个层的静态内容的内部位图表示
    napi_value CacheEnabled(napi_env env, napi_callback_info info);
    // 该值定义内部图形缓存的比例因子，范围从0.0到1.0
    napi_value CacheScale(napi_env env, napi_callback_info info);
    napi_value GetComposition(napi_env env, napi_callback_info info);
    napi_value GetLayersUnderPoint(napi_env env, napi_callback_info info);
    // 调用此方法可立即渲染当前位置
    napi_value FlushAndFenceSync(napi_env env, napi_callback_info info);
    // 播放状态改变通知
    void StateChanged(napi_env env, EnumPagPlayState state, bool bSync = true); // 新增
    // 动画重复次数通知
    static void RepeatCountfuncCallJS(napi_env env, napi_value js_callback, void *context, void *data);
    // 注册动画重复监听
    napi_value RegisterRepeatCountCallback(napi_env env, napi_callback_info info);
    // 注册当前帧数重复监听
    napi_value RegisterCurrentFrameCallback(napi_env env, napi_callback_info info);
    // 当前帧数变化通知
    static void CurrentFramefuncCallJS(napi_env env, napi_value js_callback, void *context, void *data);
    // 移除播放状态监听
    napi_value RemovePlayStateListener(napi_env env, napi_callback_info info);
    // 移除播放次数监听
    napi_value RemoveRepeatCountListener(napi_env env, napi_callback_info info);
    // 移除当前帧监听
    napi_value RemoveCurrentFrameListener(napi_env env, napi_callback_info info);

    napi_value FreeCache(napi_env env, napi_callback_info info);

public:
    
    void DestoryPlayRes();

    double Calculate(double frame);

    void OnProxyVSync(long long timestamp, void *data);

public:
    static std::unordered_map<std::string, PagPlayerProxy *> pagPlayProxyMap_;
    static std::vector<PagPlayerProxy *> pagPlayProxyVector_;
    EnumPagPlayState m_bRun;
    napi_env m_env;
    napi_ref m_refCallBackCommon;
    int64_t m_currentFrame;

    // 创建一个定时器，每隔1秒调用一次onTimer函数
    uint32_t m_totalFrame;
    uint32_t m_cur;
    double m_fCount;
    int32_t m_repeatCount; // 重复次数
    int32_t curRepeatCount; // 当前播放次数，设置重复次数有效
    double m_progress;
    int32_t m_startFrame;
    std::string id_;
    pag::PAGPlayer *m_Pagplayer;
    std::shared_ptr<pag::PAGComposition> m_PagComposition;
    int32_t m_lastDuration = 0;
    float frameRate;
    long long m_startTimes = 0;
    int m_duration = 0;
    bool isResume = false;
};
}
#endif